<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customers_Model extends CI_Model{
	protected $_name = "customers";
	protected $_primary = "CusId";
	var $Email = "Email", $FullName = "FullName", $Gender = "Gender", $Birthday = "Birthday", $Address = "Address", $Phone = "Phone", $RegisteredDate = "RegisteredDate", $Token = "Token", $CreateDateToken = "CreateDateToken", $Password = "Password", $Status = "Status";
	
	function countCustomer(){
		$this->db->where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	function checkLogin(){
		$username = $this->security->xss_clean($this->input->post('Email'));
		$password = $this->security->xss_clean($this->input->post('Password'));
		$this->db->where($this->Email, $username);
		$this->db->where($this->Password, md5($password));
		$this->db->where($this->Status, 1);
		
		$query = $this->db->get($this->_name);
		if($query->num_rows == 1){
			$row = $query->row();
			$data = array(
				"UserId" => $row->CusId,
				"FullName" => $row->FullName,
				"Email" => $row->Email,
				"state_login_default" => true
			);
			$this->session->set_userdata($data);
			return true;
		}
		return false;
	}
	function getCustomersLimit($currentPage, $number){
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->RegisteredDate, "DESC");
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function searchCustomer($keysearch, $fieldsearch){
		switch($fieldsearch){
			case 1:
				$this->db->like($this->Email, $keysearch);	
				break;
			case 2:
				$this->db->like($this->FullName, $keysearch);	
				break;
			default:
				$this->db->like($this->Email, $keysearch);	
				break;
		}
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->RegisteredDate, 'DESC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getCustomerByCusId($CusId){
		$this->db->where($this->_primary, $CusId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function updateCustomer($CusId){
		$d = explode('-', trim($this->input->post($this->Birthday, true)));
    	$Date = date('Y-m-d', mktime(0,0,0,$d[1],$d[0],$d[2]));
		$data = array(
					$this->FullName => trim($this->input->post($this->FullName, true)),
					$this->Gender => trim($this->input->post($this->Gender, true)),
					$this->Birthday => $Date,
					$this->Address => trim($this->input->post($this->Address, true)),
					$this->Phone => trim($this->input->post($this->Phone, true))
				);
		$this->db->where($this->_primary, $CusId);
		$this->db->update($this->_name, $data);
	}
	
	function changeStatusCustomer($CusId, $Status){
		try{
			$this->db->where($this->_primary, $CusId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deleteCustomer($CusId){
		try{
			$this->db->where($this->_primary, $CusId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	function insertCustomer(){
		$brithday_date = explode("/",trim($this->input->post('brithday', true)));
		$this->db->set($this->Email, trim($this->input->post('email', true)));
		$this->db->set($this->FullName, trim($this->input->post('fullname', true)));
		$this->db->set($this->Gender, trim($this->input->post('gender', true)));
		$this->db->set($this->Birthday,$brithday_date[2]."-".$brithday_date[1]."-".$brithday_date[0] );
		$this->db->set($this->Address, trim($this->input->post('address',true)));
		$this->db->set($this->Phone, trim($this->input->post('phone',true)));
		$this->db->set($this->Password, md5(trim($this->input->post('password',true))));
		$this->db->set($this->RegisteredDate, date('Y-m-d H:i:s'));
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
		$id = $this->db->insert_id();
		$data = array(
				"UserId" => $id,
				"FullName" => trim($this->input->post('fullname', true)),
				"Email" => trim($this->input->post('email', true)),
				"state_login_default" => true
			);
			$this->session->set_userdata($data);
		return $id;
	}
	
}